package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.DeptDao;
import com.itheima.domain.Dept;
import com.itheima.service.DeptService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class DeptServiceImpl implements DeptService {
    @Override
    public PageInfo<Dept> findByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<Dept> info = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            DeptDao deptDao = MybatisUtil.getMapper(sqlSession, DeptDao.class);
            PageHelper.startPage(currentPage,pageSize);
            List<Dept> list=deptDao.findAll();
            info = new PageInfo<>(list);
        } finally {
            MybatisUtil.close(sqlSession);
        }

        return info;
    }

    @Override
    public void save(Dept dept) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            DeptDao deptDao = MybatisUtil.getMapper(sqlSession, DeptDao.class);
            String id = UUID.randomUUID().toString();
            dept.setId(id);
            deptDao.save(dept);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public Dept findById(String id) {
        SqlSession sqlSession = null;
        Dept dept= null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            DeptDao deptDao = MybatisUtil.getMapper(sqlSession, DeptDao.class);
            dept = deptDao.findById(id);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return dept;
    }

    @Override
    public void update(Dept dept) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            DeptDao deptDao = MybatisUtil.getMapper(sqlSession, DeptDao.class);

            deptDao.update(dept);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            DeptDao deptDao = MybatisUtil.getMapper(sqlSession, DeptDao.class);
            Stream.of(ids).forEach(id->deptDao.delete(id));
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Dept> findAll() {
        SqlSession sqlSession = null;
        List<Dept> list = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            DeptDao deptDao = MybatisUtil.getMapper(sqlSession, DeptDao.class);
            list=deptDao.findAll();

        } finally {
            MybatisUtil.close(sqlSession);
        }

        return list;
    }
}
